package top.malaoshi.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.malaoshi.vo.JsonResult;

@ControllerAdvice
public class GlobalExceptionHandler {
    //当Controller抛出Exception异常时，由该方法处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResult handlerException(Exception e) {
        JsonResult res=new JsonResult();
        res.setCode(500);
        res.setMsg("服务器发生错误！");
        return res;
    }

    //当Controller抛出RuntimeException异常时，由该方法处理
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public JsonResult handlerRuntimeException(Exception e) {
        JsonResult res=new JsonResult();
        res.setCode(500);
        res.setMsg("服务器发生错误！");
        return res;
    }
}

